#!/usr/bin/env python
# encoding: utf-8
# @Time : 2020/4/27 9:52 
# @Author : 能量咖啡豆 
# @File : 107.py 
# @desc : 107  二叉树的层次遍历 II
class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

"""
迭代方法,给层序遍历加个层，然后翻转
"""
def solution(head):
    print("hello")
    result = []
    results = []
    roots = [head]
    rootsi = [head.val]
    result.append(roots)
    results.append(rootsi)
    i = 0
    while i < len(result):
        level = list()
        leveli = list()
        for node in result[i]:
            if node.left != None:
                level.append(node.left)
                leveli.append(node.left.val)
            if node.right != None:
                level.append(node.right)
                leveli.append(node.right.val)
        if len(level) > 0:
            result.append(level)
            results.append(leveli)
        i += 1

    return results


if __name__ == "__main__":
    print("hello")
    head = TreeNode(1)
    left = TreeNode(2)
    right = TreeNode(2)
    head.left = left
    head.right = right
    rr = solution(head)
    print(rr.reverse())
    print(rr)
    for i in range(len(rr)):
        print("--------")
        for j in range(len(rr[i])):
            print(rr[i][j])
